/**
***********************************************************************
*
* Copyright 2011 Stefan Wöhle
*
* This file is part of PixelMath
*
* PixelMath is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PixelMath is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PixelMath. If not, see <http://www.gnu.org/licenses/>.
*
***********************************************************************
*/

#include "pixelmathwindow.h"
#include "ui_pixelmathwindow.h"
#include "formulaeditor.h"
#include <QFileDialog>
#include <QMessageBox>

#define MAX_IMAGE_SIZE (float)480

/**********************************************************
 *
 *  public
 *
 **********************************************************/
PixelMathWindow::PixelMathWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::PixelMathWindow)
{
    ui->setupUi(this);

    // this applications needs at least one formulaeditor
    (new FormulaEditor(this))->show();
}

PixelMathWindow::~PixelMathWindow()
{
    delete ui;
}

const QImage& PixelMathWindow::originalImage()
{
    // return a flat copy
    return _originalImage;
}

void PixelMathWindow::setModifiedImage(QImage& modifiedImage)
{
    // take a full copy
    _modifiedImage = modifiedImage.copy();

    // show it scaled on the right
    showPreviewOnLabel(ui->labelModifiedImage,
                       _modifiedImage);
}

/**********************************************************
 *
 *  private
 *
 **********************************************************/
void PixelMathWindow::showPreviewOnLabel(QLabel* target, QImage& image)
{
    // scale so that the longest side is not longer than MAX_IMAGE_SIZE
    float scaleFactor;
    if (image.width() > image.height())
    {
        scaleFactor = image.width() / MAX_IMAGE_SIZE;
    }
    else
    {
        scaleFactor = image.height() / MAX_IMAGE_SIZE;
    }

    // do not zoom in into small images
    if (scaleFactor < 1)
        scaleFactor = 1;

    // cast the image into a pixmap an scale it (QLabel needs QPixmaps)
    target->setPixmap(
                QPixmap::fromImage(image)
                    .scaled(image.width() / scaleFactor,
                            image.height() / scaleFactor));

    qDebug("new image preview (width %dpx, height %dpx)",
           target->pixmap()->width(),
           target->pixmap()->height());
}

/**********************************************************
 *
 *  GUI "event"-handling
 *
 **********************************************************/
void PixelMathWindow::on_actionOpen_triggered()
{
    // choose the image file
    QString imageFileName = QFileDialog::getOpenFileName(this,
                                                         tr("Choose image"),
                                                         QString(),
                                                         tr("Images (*.png *.xpm *.jpg)"));
    if (QFile::exists(imageFileName)) {
        // load the image
        _originalImage = QImage(imageFileName);

        // and show it on the left
        showPreviewOnLabel(ui->labelOriginalImage,
                           _originalImage);
    }
}

void PixelMathWindow::on_actionSave_triggered()
{
    if (!_modifiedImage.isNull()) {
        // choose the target file
        QString targetFileName = QFileDialog::getSaveFileName(this,
                                                             tr("Save image to"),
                                                             QString(),
                                                             tr("Images (*.png *.xpm *.jpg)"));

        if (_modifiedImage.save(targetFileName)) {
            QMessageBox::information(this,
                                     tr("Image saved"),
                                     tr("The modified images was successfully saved to\n") + targetFileName + ".");
        } else {
            QMessageBox::warning(this,
                                     tr("Saving failed"),
                                     tr("The modified images could not been saved to\n") + targetFileName + ".");
        }
    } else {
        QMessageBox::warning(this,
                             tr("No image present"),
                             tr("There is no modified image present. Please load\n" \
                                "an image and manipulate it with the FormulaEditor\n" \
                                "before saving it."));
    }
}

void PixelMathWindow::on_actionClose_triggered()
{
    qApp->exit();
}

void PixelMathWindow::on_actionNew_formula_triggered()
{
    (new FormulaEditor(this))->show();
}


